package fr.pud.client.controller.fleet;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.model.Fleet;
import fr.pud.client.model.Vehicle;
import fr.pud.client.view.AddVehicleFrame;
import fr.pud.client.view.QueryFrame;

/**
 * 3 avr. 2012 - Projet_Ose
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes ModifyVehicleFromFleet.java
 */
public class ModifyVehicleFromFleet extends AbstractAction {
    /**
     * The default serial UID
     */
    private static final long serialVersionUID = 4308366081775629177L;

    /**
     * Default constructor.
     */
    public ModifyVehicleFromFleet() {
        this.putValue(NAME, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.MODIFY_VEHICLE));
        this.putValue(SMALL_ICON, ImagesDatabase.EDIT);
        this.putValue(SHORT_DESCRIPTION, LanguagesConstants.LANGUAGE
                .getString(LanguagesConstants.MODIFY_VEHICLE));
    }

    /**
     * Display the information inside the frame and wait for modifications.
     */
    @Override
    public void actionPerformed(ActionEvent aArg0) {
        if (Fleet.getInstance().getVehicles().size() == 0) {
            AddVehicleFrame.getInstance().getModify().setVisible(false);
            AddVehicleFrame.getInstance().getDelete().setVisible(false);
        }
        else {
            AddVehicleFrame.getInstance().getModify().setVisible(true);
            AddVehicleFrame.getInstance().getDelete().setVisible(true);
        }
        if (AddVehicleFrame.getInstance().getFleetInformation()
                .getSelectedIndex() != -1) {
            Vehicle v = (Vehicle) AddVehicleFrame.getInstance()
                    .getFleetInformation().getSelectedValue();
            AddVehicleFrame.getInstance().getCapacity()
                    .setText("" + v.getCapacity());
            AddVehicleFrame.getInstance().getMaxDriveDuration()
                    .setText("" + v.getMaxDriveDuration());
            AddVehicleFrame.getInstance().getStartingPoint()
                    .setSelectedItem(v.getStartingPoint());
            AddVehicleFrame.getInstance().getEndingPoint()
                    .setSelectedItem(v.getEndingPoint());
            AddVehicleFrame.getInstance().getAdd().setVisible(false);
            AddVehicleFrame.getInstance().getApplyModification()
                    .setVisible(true);
        }
        else {
            JOptionPane
                    .showMessageDialog(
                            QueryFrame.getInstance(),
                            LanguagesConstants.LANGUAGE
                                    .getString(LanguagesConstants.SELECT_VEHICLE_BEFORE_MODIFICATION),
                            LanguagesConstants.LANGUAGE
                                    .getString(LanguagesConstants.FLEET_INFORMATION),
                            JOptionPane.INFORMATION_MESSAGE,
                            ImagesDatabase.OSM_ICON);
        }
    }
}
